<?php

/**
 *    支付方式管理控制器
 *
 *    @author    Garbin
 *    @usage    none
 */
class PaymentApp extends BackendApp
{
    function index()
    {
        /* 读取已安装的支付方式 */
        $model_payment =& m('payment');
        $payments      = $model_payment->get_builtin(NULL, true);
        $white_list    = $model_payment->get_white_list();
        foreach ($payments as $key => $value)
        {
            $payments[$key]['system_enabled'] = in_array($key, $white_list);
        }
        $this->assign('payments', $payments);
        $this->display('payment.index.html');
    }

    /**
     *    启用
     *
     *    @author    Garbin
     *    @return    void
     */
    function enable()
    {
        $code = isset($_GET['code'])    ? trim($_GET['code']) : 0;
        if (!$code)
        {
            $this->show_warning('no_such_payment');

            return;
        }
        $model_payment =& m('payment');
        if (!$model_payment->enable_builtin($code))
        {
            $this->show_warning($model_payment->get_error());

            return;
        }
		
		$payment       = $model_payment->get_builtin_info($code);
		
		$data = array(
           'store_id'      => '0', // 不能为数值型的0，为0的话提交不了
           'payment_name'  => $payment['name'],
           'payment_code'  => $code,
           'payment_desc'  => '',
           'config'        => '',
           'is_online'     => $payment['is_online'],
           'enabled'       => 1,
           'sort_order'    => '',
        );
		
		$model_payment->add($data);

        $this->show_message('enable_payment_successed');

    }

    /**
     *    禁用
     *
     *    @author    Garbin
     *    @return    void
     */
    function disable()
    {
        $code = isset($_GET['code'])    ? trim($_GET['code']) : 0;
        if (!$code)
        {
            $this->show_warning('no_such_payment');

            return;
        }
        $model_payment =& m('payment');
        if (!$model_payment->disable_builtin($code))
        {
            $this->show_warning($model_payment->get_error());

            return;
        }
		if($model_payment->get("store_id=0 AND payment_code='{$code}'"))
		{
			$model_payment->drop("store_id=0 AND payment_code='{$code}'");
		}
        $this->show_message('disable_payment_successed');
    }
	
	/**
     *    配置
     *
     *    @author    psmoban
     *    @return    void
     */
    function conf()
    {
        $code = isset($_GET['code'])    ? trim($_GET['code']) : 0;
        if (!$code)
        {
            $this->show_warning('no_such_payment');

            return;
        }
	
		$model_payment =& m('payment');
        if (!$model_payment->in_white_list($code))
        {
            $this->show_warning('payment_disable');

            return;
        }
		/*
		if(!in_array($code, array('alipay')))
		{
			
			$this->show_warning('can_not_conf');

            return;
		}
		*/
		
		$payment       = $model_payment->get_builtin_info($code);
        if (!$payment)
        {
            $this->show_warning('no_such_payment');

            return;
        }
		
		$payment_info = $model_payment->get("store_id = 0 AND payment_code='{$code}'");
		
        if (!IS_POST)
        {
			$payment['payment_id']  =   $payment_info['payment_id'];
            $payment['payment_desc']=   $payment_info['payment_desc'];
            $payment['enabled']     =   $payment_info['enabled'];
            $payment['sort_order']  =   $payment_info['sort_order'];
            $this->assign('config', unserialize($payment_info['config']));
            $this->assign('payment', $payment);
            $this->display('payment.form.html');
        }
        else
        {
			$config = $_POST['config'];
			
			$data = array(
                'store_id'      => '0', // 不能为数值型的0，为0的话提交不了
                'payment_name'  => $payment['name'],
                'payment_code'  => $code,
                'payment_desc'  => trim($_POST['payment_desc']),
                'config'        => $config,
                'is_online'     => $payment['is_online'],
                'enabled'       => 1,
                'sort_order'    => $_POST['sort_order'],
            );
			
			if($config['fee'] && (floatval($config['fee']) <=0 || floatval($config['fee']) >=1))
			{
				$this->show_warning('fee_error');
				return;
			}
			
			if (!empty($payment_info))
			{
				if (!($model_payment->edit($payment_info['payment_id'],$data)))
				{
					$this->show_warning('edit_payment_failed');
					return;
				}
				$this->update_sys_config($code, $config);
				$this->show_message('edit_payment_successed');
			}
			else
			{
				if (!$payment_id = $model_payment->add($data))
				{
					$this->show_warning('edit_payment_failed');
	
					return;
				}
				
				$this->update_sys_config($code, $config);
				
				$this->show_message('edit_payment_successed');
			}
        }
    }
	
	// 修改用户的支付方式配置，更新系统参数
	function update_sys_config($code, $new_config)
	{
		$sys_keys = array();
		$model_payment =& m('payment');
		$payment = $model_payment->get_builtin_info($code);
		$payment_config = $payment['config'];

		if($payment_config)
		{
			// 获取系统参数键值
			foreach($payment_config as $key=>$val)
			{
				if(isset($val['configdisable']) && $val['configdisable'] == 1) {
					$sys_keys[] = $key;
				}
			}
			
			//  剔除不是系统参数的配置项
			foreach($new_config as $key=>$val)
			{
				if(!in_array($key, $sys_keys)) {
					unset($new_config[$key]);
				}
			}
		}

		// 遍历店家配置的支付方式
		$payments = $model_payment->find(array('conditions'=>'store_id > 0 AND payment_code="'.$code.'" ', 'fields'=>'config,payment_id'));
	
		foreach($payments as $key=>$payment_info)
		{
			$old_config = unserialize($payment_info['config']);
			if(is_array($old_config)) {
				$config = array_merge($old_config, $new_config);
				
				//  更新系统参数
				$model_payment->edit($payment_info['payment_id'], array('config'=>$config));
			}
		}
	}
}

?>